/*
 * TagChooserScreen.java
 *
 * Created on March 26, 2007, 3:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Activity;

import Comm.Util.Exceptions.ActivityNotFoundException;
import Experience.*;
import java.util.Vector;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.midlet.MIDlet;

/**
 *
 * @author dmgreen
 */
public class TagChooserScreen extends Form implements CommandListener, ItemStateListener {
    
    ChoiceGroup activityDescriptorCG;
    ChoiceGroup newTagCG; 
    
    private ExpUI myMIDlet; 
    private TagManager myTagManager;
    
    private Vector activityDescriptors;
    
    /**
     * Creates a new instance of TagChooserScreen
     */
    public TagChooserScreen( MIDlet expsamp, TagManager tm ) {
        super ("Choose Activity");
        
        myMIDlet = (ExpUI) expsamp;
        activityDescriptors = new Vector();
        
        activityDescriptorCG = new ChoiceGroup("", Choice.MULTIPLE);
        newTagCG = new ChoiceGroup("", Choice.EXCLUSIVE);
        newTagCG.append("Add new tag", null);
        
        myTagManager = tm;
        
        this.append("Please choose words that describe this activity:");
        this.append(activityDescriptorCG);
        //this.append(newTagCG);
        
        this.setItemStateListener(this);
        
        this.addCommand(new Command("New Tag", Command.ITEM, 1));
        this.addCommand(new Command("Next", Command.BACK, 1));
        
        this.setCommandListener(this);
        
    }
 
    public void commandAction(Command com, Displayable dis) {
	String label = com.getLabel();
	
        if ("Next".equals(label)) {
            myMIDlet.notifyNext("");
        }
   
        if ("New Tag".equals(label)) {
             Display.getDisplay(myMIDlet).setCurrent(new NewTagScreen(this));
        }
    
    }
    

    public void itemStateChanged(Item item) {
        if (item == newTagCG) {
            int sel_idx = newTagCG.getSelectedIndex();
            
            Display.getDisplay(myMIDlet).setCurrent(new NewTagScreen(this));
        
        }
    }
    
    public void addNewTags(String new_tags) {
        myTagManager.addNewTags(new_tags);
    }
    
    public void displaySelf() {
        Display.getDisplay(myMIDlet).setCurrent(this);
    }
    
    public void repopulate() {
        String[] activity_list = myTagManager.getTagNames();
        activityDescriptorCG.deleteAll();
        
        for (int i=0; i<activity_list.length; i++) {
            activityDescriptorCG.append(activity_list[i],null);
        }

    }

    /*
    public int[] getSelectedTags_id() {
        boolean[] selected_flags = new boolean[activityDescriptorCG.size()];
        int num_selected = activityDescriptorCG.getSelectedFlags(selected_flags);
        
        int[] selected_activity_ids = new int[num_selected];
        int next_idx = 0;
        
        for (int i=0; i<selected_flags.length; i++) {
            if (selected_flags[i] == true) {
                // NOTE: there is a chance that this could return -1 if somethign goes wrong.
                // should just ignore this on the server.
                int activity_id = myTagManager.getIdForTag(i);
                selected_activity_ids[next_idx] = activity_id;
                next_idx++;                    
            }
        } // END for loop
        
        return selected_activity_ids;
    } 
    */
    
    public String[] getSelectedTags() {
        boolean[] selected_flags = new boolean[activityDescriptorCG.size()];
        int num_selected = activityDescriptorCG.getSelectedFlags(selected_flags);
        
        String[] selected_activity_names = new String[num_selected];
        int next_idx = 0;
        
        for (int i=0; i<selected_flags.length; i++) {
            if (selected_flags[i] == true) {
                // NOTE: there is a chance that this could return -1 if somethign goes wrong.
                // should just ignore this on the server.
                String activity_name = myMIDlet.getTagManager().getNameForTag(i);
                                
                selected_activity_names[next_idx] = activity_name;
                next_idx++;                    
            }
        } // END for loop
        
        return selected_activity_names;
    } // END getActivityDescriptors
}
